import 'dart:io';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/constant/route.dart';
import 'package:flutter_app/constant/constant.dart';
import 'package:flutter_app/page/index.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Application',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Application'),
      routes: <String, WidgetBuilder>{
        Routes.HOME: (BuildContext context) => Home()
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  /// 倒计时5s
  var remainingTime = Constant.SPLATH_TIME;

  /// 定时器
  Timer timer;

  /// 初始化时创建定时器
  @override
  void initState() {
    super.initState();
    // 创建定时器
    timer = new Timer.periodic(const Duration(seconds: 1), this.timerCallback);
    // 测试吐司
    // Toast.show('msg');
    // 测试 SharedPreferences
    // SharedPreferencesUtil.instance.getBool('key', fun: (value) {
    //   print('默认值: $value');
    // });
    // SharedPreferencesUtil.instance.remove('isLogin');
    // SharedPreferencesUtil.instance.getKeys((value) {
    //   print(value);
    // });
  }

  /// 销毁时关闭定时器
  @override
  void dispose() {
    // 关闭定时器
    if (timer != null) {
      timer.cancel();
    }
    super.dispose();
  }

  /// 定时器回调方法
  /// 每隔1秒执行一次
  /// @param timer 定时器对象
  void timerCallback(Timer timer) {
//    print(remainingTime);
    // 更新界面数据
    setState(() {
      // 时间自减
      remainingTime--;
      // 判断剩余时间是否为0
      if (remainingTime == 0) {
        // 跳转页面
        jumpOver();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (Platform.isAndroid) {
      // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
//      SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
//      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      SystemChrome.setEnabledSystemUIOverlays([]);
//      SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
//      # 显示状态栏和虚拟按键
//      SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom]);
    }

    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage(Constant.SPLASH), // 设置背景图片
                fit: BoxFit.cover // 设置填充方式
                )), // 全屏背景
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end, // 设置主轴方向
          crossAxisAlignment: CrossAxisAlignment.end, // 设置交叉轴方向
          children: <Widget>[
            Container(
                decoration: BoxDecoration(
                    color: Color(0x44F5F5F5), // 设置背景颜色
                    borderRadius: BorderRadius.circular(10) // 设置圆角
                    ),
                margin: EdgeInsets.only(right: 20, bottom: 20),
                // 设置外边距
                height: 30,
                width: 60,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    GestureDetector(
                      onTap: jumpOver,
                      child: Text(
                        '跳过${remainingTime}s',
                        style: TextStyle(
                          fontFamily: Constant.FONT,
                          decoration: TextDecoration.none, // 去除下划线
                          color: Color(0x88FFFFFF),
                          fontSize: 16.0,
                        ),
                      ),
                    ),
                  ],
                ))
          ],
        ));
  }

  /// 跳过，进入下一个页面
  void jumpOver() {
    // 关闭定时器
    if (timer != null) {
      timer.cancel();
    }
    // 此方法跳转后无法返回到当前页面
    Navigator.of(context).pushReplacementNamed(Routes.HOME);
//    Navigator.push(this._context, new PageRouteBuilder(
//        pageBuilder: (BuildContext context, Animation<double> animation,
//        Animation<double> secondaryAnimation) {
//          return Home();
//        }, transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
//          return HomePageState.createTransition(animation, child);
//    }));
  }
}
